import { UserSwitchOutlined } from '@ant-design/icons';
import '@umijs/max';
import { history, useModel } from '@umijs/max';
import { Popconfirm } from 'antd';
import { stringify } from 'querystring';
import { flushSync } from 'react-dom';
// export type SiderTheme = 'light' | 'dark';
// export const SelectLang = () => {
//   return (
//     <UmiSelectLang
//       style={{
//         padding: 4,
//       }}
//     />
//   );
// };
export const Question = () => {
  const tenant = localStorage.getItem('tenant');
  const tenantInfo = tenant ? JSON.parse(tenant) : {};
  const { setInitialState } = useModel('@@initialState');
  const loginOut = async () => {
    flushSync(() => {
      setInitialState((s) => ({ ...s, currentUser: undefined }));
    });
    // await outLogin();
    const { search, pathname } = window.location;
    const urlParams = new URL(window.location.href).searchParams;
    /** 此方法会跳转到 redirect 参数所在的位置 */
    const redirect = urlParams.get('redirect');
    // Note: There may be security issues, please note
    if (window.location.pathname !== '/user/login' && !redirect) {
      localStorage.removeItem('token');
      localStorage.removeItem('tenant');
      history.replace({
        pathname: '/tenant',
        search: stringify({
          redirect: pathname + search,
        }),
      });
    }
  };
  const confirm = () => {
    loginOut();
  };
  return (
    <Popconfirm
      title="提示!"
      description="是否切换租户?"
      onConfirm={confirm}
      okText="是"
      cancelText="否"
    >
      <div
        style={{
          display: 'flex',
        }}
        onClick={() => {}}
      >
        <UserSwitchOutlined />
        <span
          style={{
            marginLeft: 10,
          }}
        >
          {tenantInfo?.name || '--'}
        </span>
      </div>
    </Popconfirm>
  );
};
